package {
	import com.manager.SourceManager;
	import com.view.LoadSprite;
	import com.view.MapSprite;
	import com.view.MaskSprite;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;

	[SWF(width="800",height="475")]
	public class MyMap_2 extends Sprite
	{
		public var speed:Number = 800 / 3;//800像素的 3秒缓动
		public var moveState:Boolean = false; // 移动状态
		
		private var loadSprite:LoadSprite;
		private var mapSprite:MapSprite;
		private var maskSprite:MaskSprite;
		private var roleSprite:Sprite;
		private var role:MovieClip;
		private static var _ins:MyMap_2;
		public function MyMap_2()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP;
			stage.showDefaultContextMenu=false; 
			
			mapSprite = new MapSprite();
			maskSprite = new MaskSprite();
			loadSprite = new LoadSprite();
			roleSprite = new Sprite();
			addChild(mapSprite);
			addChild(maskSprite);
			addChild(roleSprite);
			addChild(loadSprite);
			role = SourceManager.ins.role;
			roleSprite.visible = false;
			//roleSprite.addChild(role);
			role.x = 0;
			role.y = 0;
			role.gotoAndStop(1);
			roleSprite.x = 0;
			roleSprite.y = 0;
			mapSprite.mask = maskSprite;
			mapSprite.x = 0;
			mapSprite.y = 0;
			this.addEventListener(Event.ENTER_FRAME,onEnter);
			mapSprite.addEventListener(MouseEvent.CLICK,onClick);
			_ins = this;
		}
		public static function get ins():MyMap_2
		{
			if(_ins == null)
				_ins = new MyMap_2;
			return _ins;
		}
		private function onEnter(e:Event):void
		{
			this.removeEventListener(Event.ENTER_FRAME,onEnter);
			loadSprite.loadData("wwwroot/map/bj003.jpg",loadBack);
		}
		private function onClick(e:MouseEvent):void
		{
			if(e.target == role || e.target != mapSprite || moveState)
				return ;
			var mapx:Number;
			var mapy:Number;
			var movex:Number ;
			var movey:Number ;
			var mrx:Number = role.x;
			var mry:Number = role.y;
			mapx = mouseX - mapSprite.point.x;
			mapy = mouseY - mapSprite.point.y;
			if(mapx >  maskSprite.width / 2 && mapSprite.width - mapx > maskSprite.width / 2)
			{
				movex = mapx - maskSprite.width / 2;
				//mrx = maskSprite.width / 2 - role.width / 2;
			}
			else if(mapx <  maskSprite.width / 2)
			{
				movex = 0;
				mrx = mouseX;
			}
			else 
			{
				movex = mapSprite.width - maskSprite.width;
				mrx = mouseX;
			}
			if(mapy >  maskSprite.height / 2 && mapSprite.height - mapy > maskSprite.height / 2)
			{
				movey = mapy - maskSprite.height / 2;
				//mry = maskSprite.height / 2 - role.height / 2;
			}
			else if(mapy <  maskSprite.height / 2)
			{
				movey = 0;
				mry = mouseY;
			}
			else 
			{
				movey = mapSprite.height - maskSprite.height;
				mry = mouseY;
			}
			moveState = true;
			//moveRole(movex,movey);
			moveTweenMap(-movex,-movey);
			
		}
		public function loadBack(data:Object):void
		{
			loadSprite.visible = false;
			mapSprite.data = data;
			roleSprite.visible = true;
		}
		public function moveTweenMap(mx:Number,my:Number):void
		{
			if(mapSprite)
			{
				mapSprite.setTweenSite(mx,my);
			}
		}
		public function moveMap(mx:Number,my:Number):void
		{
			if(mapSprite)
			{
				mapSprite.setSite(mx,my);
			}
		}
		public function moveRole(mx:Number,my:Number):void
		{
			trace(role.x + " " + role.y); 
			role.x = mx;
			role.y = my;
		}
	}
}
